package cn.libinal.scloud.winter.service;

import cn.libinal.scloud.base.common.service.BaseJpaService;
import cn.libinal.scloud.base.common.web.DataPageRequest;
import cn.libinal.scloud.base.common.web.ResultPage;
import cn.libinal.scloud.winter.domain.ProductVo;
import cn.libinal.scloud.winter.entity.Product;
import cn.libinal.scloud.winter.entity.QProduct;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.StringExpression;
import com.querydsl.core.types.dsl.TemporalExpression;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;

/**
 * @author libinal
 */
public interface ProductService extends BaseJpaService<Product>, QuerydslBinderCustomizer<QProduct> {

    /**
     * 构建查询条件
     * @param bindings QuerydslBindings
     * @param root 查询参数
     */
    @Override
    default void customize(QuerydslBindings bindings, QProduct root) {
        bindings.bind(root.name).first(StringExpression::contains);
        bindings.bind(root.createTime).as("createTimeBefore").first(TemporalExpression::after);
        bindings.bind(root.createTime).as("createTimeAfter").first(TemporalExpression::before);
    }

    /**
     * 分页查询
     * @param predicate 查询参数
     * @param pageRequest 分页参数
     * @return ResultPage
     */
    ResultPage<ProductVo> queryByPaging(Predicate predicate, DataPageRequest pageRequest);
}
